function SDX_Events(_RootObject)
{
 this.RootObject = _RootObject ;
 this.Initialized=false;
 this.InKeySelect=false;
 this.InMouseSelect=false;
 this.OnMouseMove=OnMouseMove;
 this.OnMouseUp=OnMouseUp;
 this.OnMouseDown=OnMouseDown;
 this.OnKeyUp=OnKeyUp;
 this.OnKeyDown=OnKeyDown ;
 this.Drag=new Drag(_RootObject);
 this.DragObject=null;
 this.dragStart = dragStart;
 this.dragGo = dragGo ;
 this.dragStop = dragStop ;
 this.dragObj = new Object();
 this.dragObj.zIndex = 10;
 this.Initialize=Initialize;
 function dragStart(event, id, innerHTML, dragObject)
 {
  SDXActiveGrid = this.RootObject ;
  var el;
  var x, y;
  this.DragObject = dragObject ;
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.
  if (id)
  {
    this.dragObj.elNode = document.getElementById(id);
  }
  else
  {
    if (this.RootObject.Browser.isIE)
      this.dragObj.elNode = window.event.srcElement;
    if (this.RootObject.Browser.isNS)
      this.dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (this.dragObj.elNode.nodeType == 3)
      this.dragObj.elNode = dragObj.elNode.parentNode;
  }
  this.dragObj.elNode.innerHTML = innerHTML ;
  // Get cursor position with respect to the page.
  if (this.RootObject.Browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }

  if (this.RootObject.Browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.
  this.dragObj.cursorStartX = x;
  this.dragObj.cursorStartY = y;
  this.dragObj.elStartLeft  = window.event.X;
  this.dragObj.elStartTop   = window.event.Y;

//  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
//  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
  if (isNaN(this.dragObj.elStartLeft)) this.dragObj.elStartLeft = 0;
  if (isNaN(this.dragObj.elStartTop))  this.dragObj.elStartTop  = 0;
  this.dragObj.elNode.style.left = window.event.clientX;
  this.dragObj.elNode.style.top = window.event.clientY;
  // Update element's z-index.
  this.dragObj.elNode.style.zIndex = ++this.dragObj.zIndex;
  // Capture mousemove and mouseup events on the page.
  if (this.RootObject.Browser.isIE) {
    document.attachEvent("onmousemove", this.dragGo);
    document.attachEvent("onmouseup",   this.dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (this.RootObject.Browser.isNS) {
    document.addEventListener("mousemove", this.dragGo,   true);
    document.addEventListener("mouseup",   this.dragStop, true);
    event.preventDefault();
  }
  document.body.style.cursor = "move";
 }

 function dragGo(event)
 {
  var x, y;
  if(SDXActiveGrid.Events.dragObj.elNode.style.visibility != "visible")
    SDXActiveGrid.Events.dragObj.elNode.style.visibility = "visible"
  // Get cursor position with respect to the page.
  if (SDXActiveGrid.Browser.isIE)
  {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (SDXActiveGrid.Browser.isNS)
  {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  /// Move drag element by the same amount the cursor has moved.
  //dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  //dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
  SDXActiveGrid.Events.dragObj.elNode.style.left = window.event.clientX;
  SDXActiveGrid.Events.dragObj.elNode.style.top = window.event.clientY;
  if (SDXActiveGrid.Browser.isIE)
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (SDXActiveGrid.Browser.isNS)
    event.preventDefault();
 }

 function dragStop(event)
 {
  // Stop capturing mousemove and mouseup events.
  if (SDXActiveGrid.Browser.isIE)
  {
    document.detachEvent("onmousemove", SDXActiveGrid.Events.dragGo);
    document.detachEvent("onmouseup",   SDXActiveGrid.Events.dragStop);
    if(SDXActiveGrid.Events.dragObj.elNode != null)
        SDXActiveGrid.Events.dragObj.elNode.style.visibility = "hidden";
  }
  if (SDXActiveGrid.Browser.isNS)
  {
    document.removeEventListener("mousemove", SDXActiveGrid.Events.dragGo,   true);
    document.removeEventListener("mouseup",   SDXActiveGrid.Events.dragStop, true);
  }
  document.body.style.cursor = "auto";
  SDXActiveGrid.Events.dragObj.elNode = null;
 }
 function OnMouseMove()
 {
  if(SDXActiveGrid!=null)
   if(SDXActiveGrid.InMouseSelect==true)
    SDXActiveGrid.Table().style.cursor='move'
 }
 function OnMouseUp()
 {
  if(SDXActiveGrid!=null)
  {
   SDXActiveGrid.Table().style.cursor=''
   SDXActiveGrid.InMouseSelect=false;
  }
 }
 function OnMouseDown()
 {
  if(SDXActiveGrid!=null)
   SDXActiveGrid.InMouseSelect=true;
 }
 function OnKeyUp()
 {
  SDXCtrlSelect=false;
 }
 function OnKeyDown(e)
 {
  var nKeyCode=(navigator.appName=="Microsoft Internet Explorer"?event.keyCode:e.keyCode)
  switch(nKeyCode)
  {
   case 16:
    SDXShiftSelect=false;
    break;
   case 17:
    SDXCtrlSelect=true;
    break;
   case 13: // Enter
    if(SDXActiveGrid.Rows.EditedCell != null)
    {
        document.getElementById(SDXActiveGrid.Rows.EditedCell.HTMLID).parentNode.parentNode.parentNode.focus()
        SDXActiveGrid.Rows.EditedCell.NormalMode();
        SDXActiveGrid.Rows.EditedCell.SelectMode();
    }
    break;
   case 27: // ESC
    if(SDXActiveGrid.Rows.EditedCell != null)
    {
            SDXActiveGrid.Rows.EditedCell.CancelEdit();
            SDXActiveGrid.Rows.EditedCell.NormalMode();
            SDXActiveGrid.Rows.EditedCell.SelectMode();
    }
    break;
   case 37: // Left Arrow
    if(SDXActiveGrid.Rows.SelectedCell != null)
    {
        var row = SDXActiveGrid.Rows.SelectedCell.Row ;
        for(var nX = SDXActiveGrid.Rows.SelectedCell.ID-1;nX>-1;nX--)
        {
           if(SDXActiveGrid.Columns.Columns[nX].Visible==true)
           {
            SDXActiveGrid.Rows.SelectedCell.NormalMode();
            row.Cells.Cells[nX].SelectMode();
            return ;
           }
        }
    }
    break;
   case 38: // Up Arrow
    if(SDXActiveGrid.Rows.SelectedCell != null)
    {
        var row = SDXActiveGrid.Rows.SelectedCell.Row ;
        var rowTR = document.getElementById(row.HTMLID);
        for(var nX = rowTR.rowIndex-1;nX>-1;nX--)
        {
           var newRowTR = rowTR.parentNode.children[nX] ;
           if(newRowTR == null)
               continue ;
           if(newRowTR.object == null)
               continue;
           if(newRowTR.object.indexOf(SDXActiveGrid.Name + ".Rows")>-1) // Normal Rows
           {
            var newRow = eval(newRowTR.object).GetRowView() ;
            var cellIndex = SDXActiveGrid.Rows.SelectedCell.ID ;
            SDXActiveGrid.Rows.SelectedCell.NormalMode();
            newRow.Cells.Cells[cellIndex].SelectMode();
            return ;
           }
           else
           {
//               alert("GroupByRow")
           }
        }
    }
    break;
   case 39: // Right Arrow
    if(SDXActiveGrid.Rows.SelectedCell != null)
    {
        var row = SDXActiveGrid.Rows.SelectedCell.Row ;
        for(var nX = SDXActiveGrid.Rows.SelectedCell.ID+1;nX<SDXActiveGrid.Columns.Columns.length;nX++)
        {
           if(SDXActiveGrid.Columns.Columns[nX].Visible==true)
           {
            SDXActiveGrid.Rows.SelectedCell.NormalMode();
            row.Cells.Cells[nX].SelectMode();
            return ;
           }
        }
    }
    break;
   case 40: // Down Arrow
    if(SDXActiveGrid.Rows.SelectedCell != null)
    {
        var row = SDXActiveGrid.Rows.SelectedCell.Row ;
        var rowTR = document.getElementById(row.HTMLID);
        for(var nX = rowTR.rowIndex+1;nX<rowTR.parentNode.children.length;nX++)
        {
           var newRowTR = rowTR.parentNode.children[nX] ;
           if(newRowTR.object == null)
               continue;
           if(newRowTR.style.display == 'none')
               continue ;
           if(newRowTR.object.indexOf(SDXActiveGrid.Name + ".Rows")>-1) // Normal Rows
           {
            var newRow = eval(newRowTR.object).GetRowView() ;
            var cellIndex = SDXActiveGrid.Rows.SelectedCell.ID ;
            SDXActiveGrid.Rows.SelectedCell.NormalMode();
            newRow.Cells.Cells[cellIndex].SelectMode();
            return ;
           }
           else
           {
//               alert("GroupByRow")
           }
        }
    }
    break;
   case 113: // F2
    if(SDXActiveGrid.Rows.SelectedCell != null)
        SDXActiveGrid.Rows.SelectedCell.EditMode();
    break;
   case 46: //Delete
    if(SDXActiveGrid!=null)
    {
     SDXActiveGrid.GroupByDrager.DeleteGroupBy();
     SDXActiveGrid.Rows.DeleteSelectedRows();
    }
    break;
  }
 }
 function Drag(MainTHIS)
 {
  this.RootObject=MainTHIS;
  this.HeaderToGroupBy=HeaderToGroupBy
  function HeaderToGroupBy()
  {
   if (this.RootObject.Events.DragObject == null)
      return;
   if(this.RootObject.GroupBy.InsertColumn(this.RootObject.Events.DragObject.Key) == false)
    return;
   this.RootObject.Events.DragObject=null;
   this.RootObject.Events.dragStop(null) ;
   this.RootObject.Initialize()
  }
 }
 function Initialize()
 {
  if(this.Initialized)return;
//   document.onmousemove=this.AddDocumentFunction(document.onmousemove,this.OnMouseMove)
//   document.onmouseup=this.AddDocumentFunction(document.onmouseup,this.OnMouseUp)
//   document.onmousedown=this.AddDocumentFunction(document.onmousedown,this.OnMouseDown)
//   document.onkeyup=this.AddDocumentFunction(document.onkeyup,this.OnKeyUp)
//   document.onkeydown=this.AddDocumentFunction(document.onkeydown,this.OnKeyDown)

  document.onmousemove=this.OnMouseMove
  document.onmouseup=this.OnMouseUp
  document.onmousedown=this.OnMouseDown
  document.onkeyup=this.OnKeyUp
  document.onkeydown=this.OnKeyDown

  this.Initialized=true;
 }
}
